﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;

/// <summary>
/// 缓存存取操作
/// Add By ：zhu.xiaobing
/// Add Time :2012-08-20
/// </summary>
public class CacheManager
{
    #region 添加缓存
    /// <summary>
    /// 添加缓存
    /// </summary>
    /// <param name="key">键值</param>
    /// <param name="cacheObject">缓存对象</param>
    /// <param name="minutes">缓存时间（分钟）</param>
    public static void AddCache(string key, object cacheObject, int minutes)
    {
        HttpContext.Current.Cache.Insert(key, cacheObject, null, DateTime.Now.AddMinutes(minutes), TimeSpan.Zero);
    }

    /// <summary>
    /// 添加缓存
    /// </summary>
    /// <param name="key">键值</param>
    /// <param name="cacheObject">缓存对象</param>
    /// <param name="minutes">缓存时间（分钟）</param>
    public static void AddCacheBySeconds(string key, object cacheObject, int seconds)
    {
        HttpContext.Current.Cache.Insert(key, cacheObject, null, DateTime.Now.AddSeconds(seconds), TimeSpan.Zero);
    }

    /// <summary>
    /// 添加缓存
    /// </summary>
    /// <param name="key">键值</param>
    /// <param name="cacheObject">缓存对象</param>
    /// <param name="minutes">缓存时间（分钟）</param>
    /// <param name="cacheDependency">缓存依赖</param>
    public static void AddCache(string key, object cacheObject, int minutes, CacheDependency cacheDependency)
    {
        HttpContext.Current.Cache.Insert(key, cacheObject, cacheDependency, DateTime.Now.AddMinutes(minutes), TimeSpan.Zero);
    }
    #endregion

    /// <summary>
    /// 获取缓存
    /// </summary>
    /// <param name="key">键值</param>
    /// <returns>object</returns>
    public static object GetCache(string key)
    {
        return HttpContext.Current.Cache.Get(key);
    }

    /// <summary>
    /// 删除缓存
    /// </summary>
    /// <param name="key">键值</param>
    /// <returns></returns>
    public static object RemoveCahce(string key)
    {
        return HttpContext.Current.Cache.Remove(key);
    }
}
